package top.hkyzf.study.leetcode.string;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-1-18 11:52
 */
public class 验证回文串_125 {
    public boolean isPalindrome(String s) {
        // 处理字符串，去除其他字符，统一大小写
        StringBuilder sb = new StringBuilder();
        for (int ch : s.toLowerCase().toCharArray()) {
            if((ch >= 97 &&  ch <= 122) || (ch >= 48 && ch <= 57)) {
                sb.append((char)ch);
            }
        }
        if(sb.length() == 0) {
            return true;
        }
        String s1 = sb.toString();
        String s2 = sb.reverse().toString();
        return s1.equals(s2);
    }

    public boolean isPalindrome2(String s) {
        StringBuilder sb = new StringBuilder();
        for (char ch : s.toLowerCase().toCharArray()) {
            if(Character.isLetterOrDigit(ch)) {
                sb.append(ch);
            }
        }
        if(sb.length() == 0) {
            return true;
        }
        String result = sb.toString();
        int left = 0;
        int right = sb.length() - 1;
        while (left < right) {
            if(result.charAt(left) != result.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }


    @Test
    public void testIsPalindrome () {
        String s = "race ae car";
        System.out.println(isPalindrome2(s));
    }
}
